#include "sprite.h"

void cSprite9::draw(LPDIRECT3DDEVICE9 pDevice, LPD3DXSPRITE pSprite)
{
	if(!pDevice)
		MB("Can't draw without a valid device.");

	if(mFrames==0)
		MB("Haven't loaded a file yet.");
	if(!p_mFile)
		// Lost file try to recover
			MB("Load file...", "Who dah hell???");

	// Update position
	mPosition.x = mX;
	mPosition.y = mY;
	mPosition.z = mZ;

	if( getSprite() != true ) // copys the whole surface
	{
		gResult = pSprite->Draw(p_mFile, NULL, 0, &mPosition, D3DCOLOR_XRGB(255, 255, 255));  // draw it!
	}

	else // copy the current frame based on predetermined rects
	{
		// if scaling
		//pSprite->GetTransform(&mOriginalMatrix);

		/*
		// Screen position of the sprite w/o Z-value
		mTranslation.x = mX;
		mTranslation.y = mY;

		// out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translation

		// if not rotating
		D3DXMatrixTransformation2D(&mTemp,&mSpriteCenter,NULL,&mScaling,NULL,0.0f,&mTranslation);

		// Tell the sprite about the matrix
		pSprite->SetTransform(&mTemp);
		*/

		gResult = pSprite->Draw(p_mFile, &mRect[mCurrentFrame], NULL , &mPosition, D3DCOLOR_XRGB(255, 255, 255));  // draw it!


		// if scaling
		//pSprite->SetTransform(&mOriginalMatrix);
	}
}

	cSprite9::cSprite9()
	{

		mX = mY = mZ = 0;
		mFrames = 0; // 0 for not loaded
		mFramesX = mFramesY = 0;
		mWidth = mHeight = 0;
		mRows = mColumns = 0;

		p_mFile = NULL;

		for(int i = 0;i<12; i++)
			SetRect(&mRect[i],0,0,0,0);
	}

	cSprite9::~cSprite9()
	{
		ReleaseCOM(p_mFile);
	}

	// loads the whole file
	void cSprite9::load(LPDIRECT3DDEVICE9 pDevice, string sFile)
	{
		if(!pDevice)
			MB("Can't load a whole surface with an invalid device.");

		if(p_mFile != NULL )
			ReleaseCOM(p_mFile);

		// sprite stuff
		D3DXGetImageInfoFromFile(sFile.c_str(), &mInfo);


		gResult = D3DXCreateTextureFromFileEx(pDevice,  // IDirect3DDevice9*
			sFile.c_str(),
			mInfo.Width,
			mInfo.Height,
			1,
			D3DPOOL_DEFAULT,
			D3DFMT_A8R8G8B8,  // Specify 32-bit Alpha
			D3DPOOL_DEFAULT,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(255, 0, 255),  // Set Your Alpha Colour (This is the usual 'Hot-Pink' Colour)
			NULL,
			NULL,
			&p_mFile);  // ID3DXSprite*

		if(p_mFile==NULL)
			MB("Couldn't create texture.", sFile);

		// use the whole file
		mWidth = mInfo.Width;
		mHeight = mInfo.Height;

		// uses the whole file
		mCenter.x = 0;
		mCenter.y = 0;

		mTranslation.x = 0;
		mTranslation.y = 0;

		// if loaded setup one frame
		if(p_mFile)
			mFrames = 1;
		// end sprite stuff
	}

	// loads from file setting up animation ( i.e. sprite sheets )
	void cSprite9::load(LPDIRECT3DDEVICE9 pDevice, string sFile, int iWidth, int iHeight)
	{
		if(!pDevice)
			MB("Can't load a surface with an invalid device.");

		if(p_mFile != NULL )
			ReleaseCOM(p_mFile);

		D3DXGetImageInfoFromFile(sFile.c_str(), &mInfo);

		// Setting up frame heights
		mWidth = iWidth;
		mHeight = iHeight;

		// Find center using the whole file
		mCenter.x = mWidth/2;
		mCenter.y = mHeight/2;

		// Texture being used is 72 x 128:
		mSpriteCenter.x = mWidth/2;
		mSpriteCenter.y = mHeight/2;

		// Build our matrix to scale and position our sprite
		mScaling.x = mScaleX;
		mScaling.y = mScaleY;

		// assuming power of two
		// exampe 72/24=3
		mColumns = ( mInfo.Width / mWidth );
		mRows = ( mInfo.Height / mHeight );

		// figure out how many columns and rows to get frames
		mFrames = ( mRows * mColumns );

		mCurrentFrame = 0;


		// begin sprite stuff


		// Loading from a sheet

		gResult = D3DXCreateTextureFromFileEx(pDevice,  // IDirect3DDevice9*
			sFile.c_str(),
			mInfo.Width,
			mInfo.Height,
			1, // mip maps are bad for sprite animation math ???
			D3DPOOL_DEFAULT,
			D3DFMT_A8R8G8B8,  // Specify 32-bit Alpha  if fullscreen
			D3DPOOL_DEFAULT,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(255, 0, 255),  // Set Your Alpha Colour (This is the usual 'Hot-Pink' Colour)
			&mInfo,
			NULL,
			&p_mFile);  // ID3DXSprite*

        // Row 1
		SetRect(&mRect[0], 0, 0, mWidth, mHeight);
		SetRect(&mRect[1], mWidth, 0, (mWidth*2), mHeight);
		SetRect(&mRect[2], (mWidth*2), 0, (mWidth*3), mHeight);
		// Row 2
		SetRect(&mRect[3], 0, mHeight, mWidth, (mHeight*2));
		SetRect(&mRect[4], mWidth, mHeight, (mWidth*2), (mHeight*2));
		SetRect(&mRect[5], (mWidth*2), mHeight, (mWidth*3), (mHeight*2));
		// Row 3
		SetRect(&mRect[6], 0, (mHeight*2), mWidth, (mHeight*3));
		SetRect(&mRect[7], mWidth, (mHeight*2), (mWidth*2), (mHeight*3));
		SetRect(&mRect[8], (mWidth*2), (mHeight*2), (mWidth*3), (mHeight*3));
		// Row 4
		SetRect(&mRect[9], 0, (mHeight*3), mWidth, (mHeight*4));
		SetRect(&mRect[10], mWidth, (mHeight*3), (mWidth*2), (mHeight*4));
		SetRect(&mRect[11], (mWidth*2), (mHeight*3), (mWidth*3), (mHeight*4));

	}

	// end sprite stuff

	// Desc: Moves sprite to desired position without checking bounds
	void cSprite9::moveTo(float iX, float iY)
	{
		mPosition.x = iX;
		mPosition.y = iY;
	}

	// Desc: Moves sprite along x axis within screen space
	void cSprite9::moveX(float iX)
	{
		float temp = mX + iX;
		if(temp >= 0 && temp <= ( SCREEN_WIDTH + mWidth) )
			mX = temp;
	}

	// Desc: moves sprite along y axis within screen space
	void cSprite9::moveY(float iY)
	{
		float temp = mY + iY;
		if( temp >= 0 && temp <= ( SCREEN_WIDTH + mHeight) )
			mY = temp;
	}

	// Desc: Set sprite z value with checking
	void cSprite9::setZ(float iZ)
	{
		if( iZ >= 0.0f && iZ <= 1.0f )
			mZ = iZ;
		else
			MB("Out of range.",mFile);
	}

	void cSprite9::setScale( float scaleX, float scaleY )
	{
		if( scaleX > 0 && scaleY > 0 )
		{
			mScaleX = scaleX;
			mScaleY = scaleY;
		}
	}
